
package validation;

import utility.cardHelper;
import card.Cardset;

/**
 * Card Combination - Trio
 * Valid:	3-3-3, 4-4-4
 * Invalid: 6-6, 9-9-9-9
 * 
 */
public class Trio extends Type {

	private static Trio instance;
	
	/**
	 *  Private Constructor
	 *  Set instance to null(default)
	 */
	private Trio(){
		super();
		instance = null;
	}

	/**
	 * Implementation of Singleton
	 * @return Type instance
	 */
	public static Type getInstance(){
		if(instance == null){
			instance = new Trio();
		}
		return instance;	
	}
	
	@Override
	public void clear(){
		instance = null;
	}
	
	@Override
	public Type identify(Cardset thisHand) {
		// TODO Auto-generated method stub
		boolean isValid = false;
		int n = thisHand.count();
		if(n == 3){
			isValid = true;
			int[] histogram = new int[16];
	        for(int i=0;i<histogram.length;i++){
	        	histogram[i] = 0;
	        }
	        for(int i=0;i<n;i++){
	        	histogram[cardHelper.value2priority(thisHand.getCard(i).getValue())]++;
	        }
        	for(int i=0;i<histogram.length;i++){
        		if(histogram[i] > 0 && histogram[i] != 3){
        			isValid = false;
        		}
        	}
		}
		
		if(isValid)
			return getInstance();
		else
			return null;
	}

	@Override
	protected int getPriority(Cardset hand) {
		// TODO Auto-generated method stub
		return cardHelper.value2priority(hand.getCard(0).getValue());
	}

}
